package PrjDBConf

import (
	"dgo/MyProj/PrjConf"
	"dgo/MyProj/PrjDBUtils"
	db_utils "dgo/goutils/db-utils"
	"dgo/goutils/utils"
	"errors"
	"gitee.com/ymofen/supervalue"
)

var (
	SQL_CREATE_TABLE_RPXC_TEMPL string = `
CREATE TABLE "rpx_clt_config" (
"fid"  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT NOT NULL,
"frpxsid"  TEXT(20),
"frpxcid"  TEXT(20),
"fcaption"  TEXT(30),
"flocal_addr"  TEXT(20),
"fproxy_port"  INTEGER,
"fstate"  INTEGER,
"fremark"  TEXT(50),
"fsection"  TEXT(80)
)
`
)

type DBConfRPXC struct {
	dbconf_is_table_ok bool
	DBCONF_DB_ID       string
}

func NewDBConfRPXC(dbid string) *DBConfRPXC {
	rval := &DBConfRPXC{
		DBCONF_DB_ID: dbid,
	}
	return rval
}

func (this *DBConfRPXC) CheckEnv() error {
	if !this.dbconf_is_table_ok {
		utils.ForceCreatePath("db")
		err := PrjDBUtils.CheckCreateTableByID(this.DBCONF_DB_ID, "rpx_clt_config", SQL_CREATE_TABLE_RPXC_TEMPL)
		if err != nil {
			return err
		}
		this.dbconf_is_table_ok = true
	}
	return nil
}

func (this *DBConfRPXC) ConfUpdate(appid, typeid, conf string) error {
	return errors.New("不可以用该模式进行修改")
}

func (this *DBConfRPXC) GetConfByID(appid, strTypeID string) (error, []byte) {
	sqlB := db_utils.NewSQLBuilder()
	sqlB.SelectFields("fcaption, flocal_addr, fproxy_port, fstate, fsection").From("rpx_clt_config")
	sqlB.Where()
	sqlB.EqualValues("frpxcid", appid, "frpxsid", PrjConf.APP_UUID)

	lvsql := sqlB.String()
	golog.Debugf(lvsql)

	db, err := PrjDBUtils.GetDBConn(DBConf_DB_ID)
	if err != nil {
		return err, nil
	}
	defer db.Close()

	lst, err := db.GetRowList(lvsql)
	if err != nil {
		return err, nil
	}

	if lst == nil || len(lst) == 0 {
		return nil, nil
	}

	rval := wrapper.NewSVObject()
	lstconf := rval.ForceArrayByPath("list")
	for i := 0; i < len(lst); i++ {
		row := lst[i]
		itm := lstconf.AppendObject()
		itm.SetKeyValues("caption", row[0].AsString(""), "local_addr", row[1].AsString(""), "proxy_port", row[2].AsInt(0), "state", row[3].AsInt(0), "section", row[4].AsString(""))
	}
	return nil, rval.AsJSONBytes(false)
}

func (this *DBConfRPXC) innerGetConfList() (error, interface{}) {
	db, err := PrjDBUtils.GetDBConn(DBConf_DB_ID)
	if err != nil {
		return err, nil
	}
	defer db.Close()
	sqlB := db_utils.NewSQLBuilder()
	sqlB.SelectFields("distinct fappid").From("rpx_clt_config")
	sqlB.Where().EqualValues("frpxsid", PrjConf.APP_UUID)

	rows, err := db.GetRowList(sqlB.String())
	if err != nil {
		return err, nil
	}

	if rows == nil || len(rows) == 0 {
		return nil, nil
	}

	rval := wrapper.NewSVObject()
	lstconf := rval.ForceArrayByPath("list")
	for i := 0; i < len(rows); i++ {
		row := rows[i]
		itm := lstconf.AppendObject()
		itm.SetKeyValues("caption", row[0].AsString(""), "appid", row[0].AsString(""), "typeid", "rp-proxy", "confAs", "json")
	}
	return nil, rval
}

func (this *DBConfRPXC) GetConf(strAppID, strTypeID, strConfAsType string, newFlag int) (error, interface{}) {
	// 0 不新建, 1: 新建(如果appid对应不存在), 10: 新建Type(如果不存在则新建)

	if err := this.CheckEnv(); err != nil {
		return err, nil
	}

	if strAppID == "rpxs-list" {
		return this.innerGetConfList()
	}

	sqlB := db_utils.NewSQLBuilder()
	sqlB.SelectFields("fcaption, flocal_addr, fproxy_port, fstate, fsection").From("rpx_clt_config")
	sqlB.Where()
	sqlB.EqualValues("frpxcid", strAppID, "frpxsid", PrjConf.APP_UUID)

	lvsql := sqlB.String()
	golog.Debugf(lvsql)

	db, err := PrjDBUtils.GetDBConn(DBConf_DB_ID)
	if err != nil {
		return err, nil
	}
	defer db.Close()

	lst, err := db.GetRowList(lvsql)
	if err != nil {
		return err, nil
	}

	if lst == nil || len(lst) == 0 {
		if newFlag == 1 {
			recUp := db_utils.NewRecUpdateUpbuilder()
			recUp.SetValues("frpxsid", PrjConf.APP_UUID, "frpxcid", strAppID)
			PrjDBUtils.PushDBExecQueue(func(db *db_utils.DBConn, rec *PrjDBUtils.Db_exec_rec) error {
				if db != nil {
					db.ExecSql(recUp.BuildInsert("rpx_clt_config"))
				}
				return nil
			})
		}
		return nil, nil
	}

	rval := wrapper.NewSVObject()
	lstconf := rval.ForceArrayByPath("list")
	for i := 0; i < len(lst); i++ {
		row := lst[i]
		itm := lstconf.AppendObject()
		itm.SetKeyValues("caption", row[0].AsString(""), "local_addr", row[1].AsString(""), "proxy_port", row[2].AsInt(0), "state", row[3].AsInt(0), "section", row[4].AsString(""))
	}
	return nil, rval
}
